package com.ordos.recruit.module.user.controller;

import cn.hutool.core.lang.UUID;
import com.ordos.recruit.module.user.entity.*;
import com.ordos.recruit.module.user.exception.UserLoginException;
import com.ordos.recruit.module.user.helper.SsoLoginStoreHelper;
import com.ordos.recruit.module.user.helper.SsoSessionIdHelper;
import com.ordos.recruit.module.user.helper.SsoTokenLoginHelper;
import com.ordos.recruit.module.user.service.UserService;
import com.ordos.recruit.module.user.util.UserUtil;
import com.wonders.commons.util.exception.ApiException;
import com.wonders.commons.util.model.ValidatorResult;
import com.wonders.commons.util.response.ApiResponse;
import com.wonders.commons.validation.ObjectValidator;
import org.springframework.web.bind.annotation.*;

/**
 * 用户相关的Controller.
 *
 * @author jin
 */
@RestController
@RequestMapping("user")
public class UserController {

    private final UserService userService;
    private final ObjectValidator objectValidator;

    public UserController(UserService userService, ObjectValidator objectValidator) {
        this.userService = userService;
        this.objectValidator = objectValidator;
    }

    @PostMapping("saveUser")
    public ApiResponse<String> saveUser(@RequestBody SysUserDto sysUserDto) {
        //保存用户信息.
        userService.saveSysUser(sysUserDto);
        return ApiResponse.success("用户保存成功");

    }

    @PostMapping("getUserInfo")
    public ApiResponse<SysSsoUserDto> getUserInfo() {
        SysSsoUserDto userByRequest = UserUtil.getUserByRequest();

        if (userByRequest == null) {
            throw new UserLoginException("获取用户信息失败， 请检查当前Cookie状态.");
        }

        return ApiResponse.success(userByRequest);
    }

    @PostMapping("updatePassword")
    public ApiResponse<String> updatePassword(@RequestBody SysUserUpdatePwdDto sysUserDto) {
        userService.updatePwd(sysUserDto);
        return ApiResponse.success("用户修改成功");
    }

    /**
     * Login 用户
     *
     * @param sysUserDto sysUserDto
     * @return ReturnT
     */
    @PostMapping("loginUser")
    public ApiResponse<SysUserLoginSuccessVo> login(@RequestBody SysUserLoginDto sysUserDto) {
        //校验请求入参参数.
        ValidatorResult<SysUserLoginDto> sysUserLoginDto = objectValidator.objectValidator(sysUserDto);
        if(sysUserLoginDto.isHasErrors()){
            throw new ApiException(ApiResponse.dataError(0, sysUserLoginDto.getError()));
        }
        // valid login
        SysUserPo sysUserPo = userService.getSysUserPo(sysUserDto.getPhoneNumber(), sysUserDto.getPassword());

        // 1、make sso user
        SysSsoUserDto ssoUserDto = new SysSsoUserDto();
        ssoUserDto.setUserid(String.valueOf(sysUserPo.getId()));
        ssoUserDto.setUsername(sysUserPo.getName());
        ssoUserDto.setVersion(UUID.fastUUID().toString(true));
        ssoUserDto.setExpireMinute(SsoLoginStoreHelper.getRedisExpireMinute());
        ssoUserDto.setExpireFreshTime(System.currentTimeMillis());
        ssoUserDto.setCardId(sysUserPo.getCardId());
        ssoUserDto.setPhoneNumber(sysUserPo.getPhoneNumber());
        ssoUserDto.setUserIsAdmin(sysUserPo.getUserIsAdmin());

        // 2、generate sessionId + storeKey
        String sessionId = SsoSessionIdHelper.makeSessionId(ssoUserDto);

        // 3、login, store storeKey
        SsoTokenLoginHelper.login(sessionId, ssoUserDto);

        // 4、return sessionId
        return ApiResponse.success(new SysUserLoginSuccessVo(sessionId));
    }

}
